<?php
// +----------------------------------------------------------------------
// | JuhePHP [ NO ZUO NO DIE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Juhedata <info@juhe.cn-->
// +----------------------------------------------------------------------
 
//----------------------------------
// 汇率调用示例代码 － 聚合数据
// 在线接口文档：http://www.juhe.cn/docs/80
//----------------------------------
 
header('Content-type:text/html;charset=utf-8');

include_once "ezsql/ez_sql_core.php";
include_once "ezsql/ez_sql_mysqli.php";

$db = new ezSQL_mysqli('root','aa0e5dc2a35be93e','socket','127.0.0.1');


//************金十数据历史数据************


$code = $db->get_results("SELECT code,add_time FROM `s_name`;");
foreach($code as $key){
	print_r($key);
	echo $key->code;
}
exit;




$url = "https://chart.jin10.com/baojia.php";
$params = array(
      "type" => "gbpusd",
	  "_" => time() . rand(1,999999),
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);

$content = str_replace(array("(", ")"), "", $content);
$result = json_decode($content,true);
if($result){
    foreach($result as $key){
		$key[2] = $key[0] / 1000;
		$key[3] = date("Y-m-d H:i:s", $key[2]);
		$info = $db->query("INSERT INTO `socket`.`s_sdata` (`code`, `time`, `times`, `price`) VALUES ('".$params["type"]."', '".$key[2]."', '".$key[3]."', '".$key[1]."');");
		print_r($info);
		unset($info);
	}
}else{
    echo "请求失败";
}
//**************************************************


/**
	历史数据采集； 2017/5/7
**/
function jin10_history($type){
	$url = "https://chart.jin10.com/baojia.php";
	$params = array(
		  "type" => $type,
		  "_" => time() . rand(1,999999),
	);
	$paramstring = http_build_query($params);
	$content = juhecurl($url,$paramstring);

	$content = str_replace(array("(", ")"), "", $content);
	$result = json_decode($content,true);
	if($result){
		foreach($result as $key){
			$key[2] = $key[0] / 1000;
			$key[3] = date("Y-m-d H:i:s", $key[2]);
			$info = $db->query("INSERT INTO `socket`.`s_sdata` (`code`, `time`, `times`, `price`) VALUES ('".$params["type"]."', '".$key[2]."', '".$key[3]."', '".$key[1]."');");
			print_r($info);
			unset($info);
		}
	}else{
		echo "请求失败";
	}
}



exit;


//配置您申请的appkey
$appkey[] = "5437f942f2b037cf64deb8d501418bb7";
$appkey[] = "03e971239c591a856761f079a276cd5d";
$appkey[] = "e6433bf797b9572d18fae0c9247d794e";
$appkey[] = "09ff0501cea42426c20b0635a0623d2f";
$appkey[] = "47e27ba2819ff652257eb855159ffe6e";
$appkey[] = "ebcc5c7bbf06b8c1bc2d1557dd6a9dc0";
$appkey[] = "0a7af06fbeb3585450ba9b7559494446";


$appkey = $appkey[rand(0,6)];


//************1.常用汇率查询************
$url = "http://op.juhe.cn/onebox/exchange/query";
$params = array(
      "key" => $appkey,//应用APPKEY(应用详细页查询)
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 

 
//************2.货币列表************
$url = "http://op.juhe.cn/onebox/exchange/list";
$params = array(
      "key" => $appkey,//应用APPKEY(应用详细页查询)
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 /*
//************3.实时汇率查询换算************
$url = "http://op.juhe.cn/onebox/exchange/currency";
$params = array(
      "from" => "",//转换汇率前的货币代码
      "to" => "",//转换汇率成的货币代码
      "key" => $appkey,//应用APPKEY(应用详细页查询)
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 */
 
 
 
 
/**
 * 请求接口返回内容
 * @param  string $url [请求的URL地址]
 * @param  string $params [请求的参数]
 * @param  int $ipost [是否采用POST形式]
 * @return  string
 */
function juhecurl($url,$params=false,$ispost=0){
    $httpInfo = array();
    $ch = curl_init();
 
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    if( $ispost )
    {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}



/****
--
-- Database: `socket`
--

-- --------------------------------------------------------

--
-- 表的结构 `s_kdata`
--

DROP TABLE IF EXISTS `s_kdata`;
CREATE TABLE IF NOT EXISTS `s_kdata` (
  `id` int(11) NOT NULL,
  `code` varchar(26) NOT NULL,
  `time` int(11) NOT NULL,
  `times` datetime NOT NULL,
  `high` decimal(10,5) NOT NULL COMMENT '最高价',
  `low` decimal(10,5) NOT NULL COMMENT '最低价',
  `open` decimal(10,5) NOT NULL COMMENT '开盘价',
  `close` decimal(10,5) NOT NULL COMMENT '收盘价',
  `addtime` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='k line';

--
-- 插入之前先把表清空（truncate） `s_kdata`
--

TRUNCATE TABLE `s_kdata`;
-- --------------------------------------------------------

--
-- 表的结构 `s_name`
--

DROP TABLE IF EXISTS `s_name`;
CREATE TABLE IF NOT EXISTS `s_name` (
  `id` int(11) NOT NULL,
  `name` varchar(26) NOT NULL COMMENT '产品名',
  `code` varchar(26) NOT NULL COMMENT '产品编码',
  `type` int(11) NOT NULL DEFAULT '3' COMMENT '1 期货 2 货币 3 股票',
  `start_time` int(11) DEFAULT NULL COMMENT '开市时间',
  `stop_time` int(11) DEFAULT NULL COMMENT '关市时间',
  `add_time` int(11) DEFAULT NULL COMMENT '增加时间',
  `note` text COMMENT '备注'
) ENGINE=MyISAM AUTO_INCREMENT=24 DEFAULT CHARSET=utf8 COMMENT='产品表';

--
-- 插入之前先把表清空（truncate） `s_name`
--

TRUNCATE TABLE `s_name`;
--
-- 转存表中的数据 `s_name`
--

INSERT INTO `s_name` (`id`, `name`, `code`, `type`, `start_time`, `stop_time`, `add_time`, `note`) VALUES
(1, '现货黄金', 'xauusd', 1, 0, 0, 0, 'jin10'),
(2, '现货白银', 'xagusd', 1, NULL, NULL, NULL, NULL),
(3, '布伦特油', 'ukoil', 1, NULL, NULL, NULL, NULL),
(4, '美国原油', 'usoil', 1, NULL, NULL, NULL, NULL),
(5, '美元指数', 'dxy', 1, NULL, NULL, NULL, NULL),
(6, '欧元美元', 'eurusd', 1, NULL, NULL, NULL, NULL),
(7, '天然气', 'ngas', 1, NULL, NULL, NULL, NULL),
(8, '英镑美元', 'gbpusd', 2, NULL, NULL, NULL, NULL),
(9, '美元日元', 'usdjpy', 2, NULL, NULL, NULL, NULL),
(10, '澳元美元', 'audusd', 2, NULL, NULL, NULL, NULL),
(11, '美元瑞郎', 'usdchf', 2, NULL, NULL, NULL, NULL),
(12, '纽元美元', 'nzdusd', 2, NULL, NULL, NULL, NULL),
(13, '离岸人民币', 'usdcnh', 2, NULL, NULL, NULL, NULL),
(14, '现货钯金', 'xpdusd', 2, NULL, NULL, NULL, NULL),
(15, '现货铜', 'copper', 2, NULL, NULL, NULL, NULL),
(16, '道琼斯', 'dowi', 3, NULL, NULL, NULL, NULL),
(17, '纳斯达克', 'nasx', 3, NULL, NULL, NULL, NULL),
(18, '标普500', 'spx500', 3, NULL, NULL, NULL, NULL),
(19, '日经225', 'jpn225', 3, NULL, NULL, NULL, NULL),
(20, '上证综指', 'szzz', 3, NULL, NULL, NULL, NULL),
(21, '深证成指', 'szcz', 3, NULL, NULL, NULL, NULL),
(22, '现货铂金', 'xptusd', 3, NULL, NULL, NULL, NULL),
(23, '美债', 'bund', 3, NULL, NULL, NULL, NULL);

-- --------------------------------------------------------

--
-- 表的结构 `s_sdata`
--

DROP TABLE IF EXISTS `s_sdata`;
CREATE TABLE IF NOT EXISTS `s_sdata` (
  `id` int(11) NOT NULL,
  `code` varchar(26) NOT NULL,
  `time` int(11) NOT NULL,
  `times` datetime NOT NULL,
  `price` float NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- 插入之前先把表清空（truncate） `s_sdata`
--

TRUNCATE TABLE `s_sdata`;
--
-- Indexes for dumped tables
--

--
-- Indexes for table `s_kdata`
--
ALTER TABLE `s_kdata`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `s_name`
--
ALTER TABLE `s_name`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `s_sdata`
--
ALTER TABLE `s_sdata`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `s_kdata`
--
ALTER TABLE `s_kdata`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `s_name`
--
ALTER TABLE `s_name`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=24;
--
-- AUTO_INCREMENT for table `s_sdata`
--
ALTER TABLE `s_sdata`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;


****/

?>